<?php
/*
    My College Link Database Functions
    Dan Newman

    DBFunctions.php
    8/23/2011
 */

include('utilityFunctions.php');

function getDatabase()
{
    $myServer = "mycollegelink.db.7726125.hostedresource.com";
    $myUser = "mycollegelink";
    $myPass = "MyCL500777";
    $myDB = "mycollegelink";

    $connectionInfo = array("Database"=> $myDB, "UID"=> $myUser, "PWD"=> $myPass);

    $connection = mysql_connect($myServer, $myUser, $myPass) or die ("Connection To Database Failed");
    $myDB = mysql_select_db($myDB, $connection) or die ("Problem selecting database: " . $mysql_error($connection));
    
    return  $connection;
}

function executeQuery($query){
    $connection = getDatabase();
    $queryResults = mysql_query($query, $connection) or die (mysql_error($connection));

    while ($row = mysql_fetch_array($queryResults, MYSQL_ASSOC)){
        $results[]= $row;
    }

    mysql_free_result($queryResults);
    mysql_close($connection);

    return $results;
}

function executeNonQuery($query){
    $connection = getDatabase();
    
    mysql_query($query, $connection) or die (mysql_error($connection));
    mysql_close($connection);
}

//escape all single quote characters in the middle of a string
function excapeSingleQuotes($str){
	return str_replace('\'', '\'\'', trim($str));
}

//returns 2 strings for first and last name
function getFirstLastString($userID){
  $query = <<<STR
  Select First, Last
  From Users
  Where  UserID = '$userID'
STR;
  
  $res = executeQuery($query);
  if(isset($res)){
	  return $res[0];
  }
  return null;
}

//returns the description for the students bio page
function getDescriptionString($uid){
	$query = <<<STR
	select HSName
	from HighSchool
	where HighSchoolID in 
		(Select HighSchoolID
		 From Users
		 Where UserID = $uid)
STR;
	$res = executeQuery($query);
	if(isset($res)){
		return $res[0]['HSName'];
	}
	return null;	
}

//return all the categories in the category table
function getCategoryList(){
	$query = <<<STR
	select CategoryID, Type
	from Category
STR;
	return executeQuery($query);
}

//return all the operating systems recognized by the database
function getOSList(){
  $query = <<<STR
   select OSID, OpSys
   From OS
STR;
  return executeQuery($query);
}

//return all the Software titles in the DB
function getSoftwareList(){
	$query = <<<STR
	select SoftwareID, Title
	from Software
STR;
	return executeQuery($query);
}

?>